package site.user.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyEquity;
import modules.recruit.form.ApplyEquityForm;
import modules.recruit.service.ApplyEquityService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.service.EntityService;

public class ApplyEquityActionSite extends ApplyCommonStep {

	private ApplyEquityService applyEquityService;

	public EntityService getEntityService() {
		return applyEquityService;
	}

	public ApplyEquityService getApplyEquityService() {
		return applyEquityService;
	}

	public void setApplyEquityService(ApplyEquityService applyEquityService) {
		this.applyEquityService = applyEquityService;
	}
	
	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form,request);
		if(entity == null && applyBase != null){
			entity = applyBase.getEquity();
		}
		//不是由ID参数得到的entity
		if (id == null && entity != null) {
			ApplyEquity applyEquity = (ApplyEquity)entity;
			ApplyEquityForm applyEquityForm = (ApplyEquityForm) form;
			applyEquityForm.setId(applyEquity.getId());
			id = applyEquity.getId();
		}
	}

	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		bindForm(form, entity);
		ApplyEquityForm applyEquityForm = (ApplyEquityForm)form;
		applyEquityForm.setApplyBaseId(applyBaseId);
		
		return handleEdit(mapping,form,request,response);
	}
	
	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request, form);
		if(isCreateOperation){
			ApplyEquity applyEquity = (ApplyEquity) entity;
			applyEquity.setBase(applyBase);
			entity = applyEquity;
		}
	}

	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (!validateForm(mapping, form, request))
			return handleFormErrors(mapping, form, request, response);
		
		doInitEntity(request, form);

		doSaveEntity(request, form);

		String direction = request.getParameter("direction");
		if (direction != null && direction.equals("next"))
			return new ActionForward(
					"/site/user/applyFinance.do?method=edit&applyBaseId="
							+ applyBaseId.toString(), true);
		else if(direction != null && direction.equals("last"))
			return new ActionForward(
					"/site/user/apply.do?method=edit&id="
							+ applyBaseId.toString(), true);
		else{
			saveMessage(request, "entity.saved");
			ApplyEquityForm applyEquityForm = (ApplyEquityForm) form;
			applyEquityForm.setId((Long)id);
			return handleEdit(mapping,form,request,response);
		}
	}

}
